package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.ruoyi.system.domain.OaOtheraPurchase;
import com.ruoyi.system.domain.OaProjectInitiation;
import com.ruoyi.system.service.IOaProjectInitiationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.OaPaySumMapper;
import com.ruoyi.system.domain.OaPaySum;
import com.ruoyi.system.service.IOaPaySumService;

/**
 * 应付汇总去付款Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-18
 */
@Service
public class OaPaySumServiceImpl implements IOaPaySumService
{
    @Autowired
    private OaPaySumMapper oaPaySumMapper;
    @Autowired
    private IOaProjectInitiationService oaProjectInitiationService;
    /**
     * 查询应付汇总去付款
     *
     * @param id 应付汇总去付款主键
     * @return 应付汇总去付款
     */
    @Override
    public OaPaySum selectOaPaySumById(Long id)
    {
        return oaPaySumMapper.selectOaPaySumById(id);
    }

    /**
     * 查询应付汇总去付款列表
     *
     * @param oaPaySum 应付汇总去付款
     * @return 应付汇总去付款
     */
    @Override
    public List<OaPaySum> selectOaPaySumList(OaPaySum oaPaySum)
    {
        return oaPaySumMapper.selectOaPaySumList(oaPaySum);
    }

    /**
     * 新增应付汇总去付款
     *
     * @param oaPaySum 应付汇总去付款
     * @return 结果
     */
    @Override
    public int insertOaPaySum(OaPaySum oaPaySum)
    {
        oaPaySum.setCreatedDate(new Date());
        int i = oaPaySumMapper.insertOaPaySum(oaPaySum);
        //更新金额
        Long initiationId = oaPaySum.getInitiationId();
        OaPaySum oaOther2 = new OaPaySum();
        oaOther2.setInitiationId(initiationId);
        List<OaPaySum> oaOthers1 = oaPaySumMapper.selectOaPaySumList(oaOther2);
        a(initiationId,oaOthers1);
        return i;
    }
    public void a(Long initiationId,List<OaPaySum> oaOthers1 ) {
        OaProjectInitiation oaProjectInitiation = oaProjectInitiationService.selectOaProjectInitiationById(initiationId.intValue());
        BigDecimal paidAmount = BigDecimal.ZERO;
        for (OaPaySum oaOther : oaOthers1) {
            paidAmount = paidAmount.add(oaOther.getPaymentAmount());
        }
        oaProjectInitiation.setTotalPaymentAmount(paidAmount);
        oaProjectInitiationService.updateOaProjectInitiation(oaProjectInitiation);
    }
    /**
     * 修改应付汇总去付款
     *
     * @param oaPaySum 应付汇总去付款
     * @return 结果
     */
    @Override
    public int updateOaPaySum(OaPaySum oaPaySum)
    {
        return oaPaySumMapper.updateOaPaySum(oaPaySum);
    }

    /**
     * 批量删除应付汇总去付款
     *
     * @param ids 需要删除的应付汇总去付款主键
     * @return 结果
     */
    @Override
    public int deleteOaPaySumByIds(Long[] ids)
    {
        return oaPaySumMapper.deleteOaPaySumByIds(ids);
    }

    /**
     * 删除应付汇总去付款信息
     *
     * @param id 应付汇总去付款主键
     * @return 结果
     */
    @Override
    public int deleteOaPaySumById(Long id)
    {
        return oaPaySumMapper.deleteOaPaySumById(id);
    }

    @Override
    public void delete(Integer id) {
        oaPaySumMapper.delete(id);
    }
}
